package com.deng.common.view
{	
	
	import com.deng.common.model.CommonModelLocator;
	import com.deng.common.vo.OrganizationVo;
	import com.deng.util.GlobalProperties;
	
	import mx.collections.ArrayCollection;
	import mx.collections.ICollectionView;
	import mx.controls.Alert;
	import mx.controls.treeClasses.ITreeDataDescriptor;

	public class OrganizationTreeProvider implements ITreeDataDescriptor
	{		
		private function getChildArr(pVo:OrganizationVo):ArrayCollection{	
			trace("getChildArr ....................");		
			try{
				var childArr:ArrayCollection = new ArrayCollection();
				for(var i:int = 0 ; i < CommonModelLocator.getInstance().organizationTable.length;i++){
					var vo:OrganizationVo = CommonModelLocator.getInstance().organizationTable.getItemAt(i) as OrganizationVo;
					if(vo.parent_id == pVo.id){
						childArr.addItem(vo);
					}
				}
				return childArr;
			}catch(err:Error){
				Alert.show(err.getStackTrace());
			}			
			return null;
		}
		
		public function OrganizationTreeProvider()
		{
			trace("OrganizationTreeProvider ....................");
		}

		public function getChildren(node:Object, model:Object=null):ICollectionView
		{
			trace("getChildren ....................");
			try{
				if(node is OrganizationVo){
					var vo:OrganizationVo = OrganizationVo(node);
					if(vo.is_leaf == GlobalProperties._IS_LEAF){
						return null;
					}
					return getChildArr(vo);
				}
			}catch(err:Error){
				Alert.show(err.getStackTrace());
			}
			return null;
		}
		
		public function hasChildren(node:Object, model:Object=null):Boolean
		{
			trace("hasChildren ....................");
			try{
				if(node is OrganizationVo){
					var vo:OrganizationVo = OrganizationVo(node);
					if(vo.is_leaf == GlobalProperties._IS_LEAF){
						return false;
					}
					var chilrenL:ArrayCollection = getChildArr(vo);
					if(chilrenL != null && chilrenL.length > 0){
						return true;
					}
				}
			}catch(err:Error){
				Alert.show(err.getStackTrace());
			}
			
			return false;
		}
		
		public function isBranch(node:Object, model:Object=null):Boolean
		{
			trace("isBranch ...................."+node+"..");
			try{
				if(node == null || node == ""){
					return false;
				}
				if(node is OrganizationVo){
					var vo:OrganizationVo = OrganizationVo(node);
					if(vo.child_organization_arr != null && vo.child_organization_arr.length > 0){
						return true;
					}else{
						vo.child_organization_arr = getChildArr(vo);
						if(vo.child_organization_arr != null && vo.child_organization_arr.length > 0){
							return true;
						}
					}					
				}
			}catch(err:Error){
				Alert.show(err.getStackTrace());
			}			
			return false;
		}
		
		public function getData(node:Object, model:Object=null):Object
		{
			trace("getData ....................");
			return null;
		}
		
		public function addChildAt(parent:Object, newChild:Object, index:int, model:Object=null):Boolean
		{
			trace("addChildAt ....................");
			return false;
		}
		
		public function removeChildAt(parent:Object, child:Object, index:int, model:Object=null):Boolean
		{
			trace("removeChildAt ....................");
			return false;
		}
	}
}